/*
 * @Author: yuyunhao
 * @Date: 2021-10-12 17:53:49
 * @LastEditTime: 2021-10-19 18:08:53
 * @LastEditors: yuyunhao
 * @Description: 
 * @FilePath: \react_try\webpack.config.js
 * 代码都是复制过来的，怎么会出错
 */
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// const ESLintPlugin = require('eslint-webpack-plugin');

module.exports = {
  entry: './src/main.js',
  output: {
    filename: 'built.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '',
  },
  module: {
    rules: [
      // 处理less文件
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader',
          {
            loader: "postcss-loader",
            options: {
              postcssOptions: {
                plugins: [
                  [
                    "postcss-preset-env",
                    {
                      // Options
                    },
                  ],
                ],
              },
            },
          },
        ]
      },
      // 处理图片文件
      {
        test: /\.(jpg|png|gif)$/,
        loader: 'url-loader',
        options: {
          limit: 8 * 1024,
          esModule: false
        }
      },
      // 处理html图片问题
      {
        test: /\.html$/,
        loader: 'html-loader'
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ["babel-loader"]
      },
      // 处理其他资源
      {
        exclude: /\.(css|html|js|jsx)$/,
        loader: 'file-loader'
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.join(__dirname, './public/index.html') // 指定模板路径
    }),
    new CleanWebpackPlugin() ,
    // new ESLintPlugin()
  ],
  devServer: {
    historyApiFallback: true,
    host: '0.0.0.0',
    hot: true,
    port: 3000,
    compress: true,
    client: {
      webSocketURL: 'ws://osc.gitee.work/xly-poc/webide/workbench/11593/port/3000/ws',
    },
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
    }
  },
  resolve: {
    alias: {
      "@src":path.resolve("src"),
      "@components":path.resolve("src/components"),
      "@pages":path.resolve("src/pages"),
      "@utils":path.resolve("src/utils"),
      "@services":path.resolve("src/services"),
      "@routers":path.resolve("src/routers"),
      "@styles":path.resolve("src/styles"),
    },
  },
  mode: 'development'
};